package com.example.permissionservice.servletComponent;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;


/**
 *  自定义Filter类
 */
@WebFilter
public class MyFilter implements Filter {
    private static final Logger logger = LoggerFactory.getLogger(MyFilter.class);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
                         FilterChain filterChain) throws IOException, ServletException {
        String address = null;
        String method = null;
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        try {
            address = httpServletRequest.getRequestURI();
            method = httpServletRequest.getMethod();
        }catch (Exception e){
            e.printStackTrace();
        }

        int port = servletRequest.getLocalPort();
//        logger.info("进入访问过滤器，访问的url:{}，访问的方法：{}", request.getRequestURL(), request.getMethod());
        logger.info("请求经过[permission-service-provider-1]的过滤器，请求的地址为:{}，该服务的端口为:{}，请求的方法为:{}", address, port, method);
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}
